package com.suse.lhw.hms.authserver.config;

import com.suse.lhw.hms.authserver.handler.FailureAurhorizationHandler;
import com.suse.lhw.hms.authserver.handler.SuccsseAuthorizaionHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.BeanIds;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

//import org.springframework.security.crypto.factory.PasswordEncoderFactories;

/**
 * @ProjectName: demo
 * @Package: com.example.authorityserver.config
 * @ClassName: WebCecurityConfiguration
 * @Description: java类作用描述
 * @Author: 李鸿炜
 * @CreateDate: 2020/2/17 17:37
 * @UpdateUser: 更新者
 * @UpdateDate: 2020/2/17 17:37
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true, jsr250Enabled = true)
public class WebCecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Bean(name = BeanIds.AUTHENTICATION_MANAGER)
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    @Bean
    public UserDetailsService userDetailsService() {
        return new UserDetailsServiceImpl();
    }

    @Autowired
    SuccsseAuthorizaionHandler succsseAuthorizaionHandler;
    @Autowired
    FailureAurhorizationHandler failureAurhorizationHandler;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        /**
         * @Author:Hongwei Li
         * @Description:用于指定自定义登录界面
         * @Date:18:41 2020/3/31
         * @Param:[http]
         * @Return:void
         **/
//        http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
//        // 定制我们自己的 session 策略：调整为让 Spring Security 不创建和使用 session
//        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
//        // 请求进行拦截 验证 accessToken
//
        http
                .authorizeRequests()
                .antMatchers("/toIndex", "/templates/index.html", "/css/**", "/js/**", "/image/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                // 设置自定义登录的页面
                .loginPage("/toIndex")
                // 登录页表单提交的 action（th:action="@{/my-login}"） URL
                .loginProcessingUrl("/authentication/form")
                .usernameParameter("username")
                .passwordParameter("password")
                .successHandler(succsseAuthorizaionHandler)
                .failureHandler(failureAurhorizationHandler)
                //.defaultSuccessUrl("/hello", true)
                .and().csrf().disable()
        ;


    }

    @Override
    public void configure(AuthenticationManagerBuilder web) throws Exception {
        /**
         * @Author:Hongwei Li
         * @Description:使用userDetailsService来从数据库拉信息验证
         * @Date:8:52 2020/4/3
         * @Param:[web]
         * @Return:void
         **/
        // PasswordEncoderFactories.createDelegatingPasswordEncoder();
        web.userDetailsService(userDetailsService()).passwordEncoder(passwordEncoder());
    }


//    @Override
//    protected  void configure(HttpSecurity httpSecurity) throws Exception{
//        /**
//         * @Author:Hongwei Li
//         * @Description:关闭security的安全认证
//         * @Date:16:55 2020/3/27
//         * @Param:[httpSecurity]
//         * @Return:void
//        **/
//        httpSecurity.authorizeRequests().anyRequest().permitAll().and().logout().permitAll();
//    }
}
